package com.wxisme.ssm.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wxisme.ssm.po.User;
import com.wxisme.ssm.po.UserInfo;
import com.wxisme.ssm.service.UserInfoService;
import com.wxisme.ssm.service.UserService;

/**
 *用户信息相关的操作
 *@author wxisme
 *@time 2015-9-25 下午3:55:18
 */
@Controller
public class UserInfoController {
	
	@Autowired
	private UserInfoService userInfoService;
	
	@Autowired
	private UserService userService;
	
	
	/**
	 * 查找用户信息
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryUserInfo.action")
	public ModelAndView queryUserInfo(HttpSession session) throws Exception {
		
		ModelAndView modelAndView = new ModelAndView();
		
		UserInfo userInfo = new UserInfo();
		
		String username = (String) session.getAttribute("username");
		
		userInfo = userInfoService.selectUserInfoByUsername(username);
		
		modelAndView.addObject("userInfo", userInfo);
		
		modelAndView.setViewName("userinfo");
		
		return modelAndView;
	}
	
	
	/**
	 * 查找用户好友信息
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryFriendInfo.action")
	public ModelAndView queryFriendInfo(HttpSession session) throws Exception {
		
		ModelAndView modelAndView = new ModelAndView();
		
		UserInfo userInfo = new UserInfo();
		
		String friendname = (String) session.getAttribute("friendname");
		
		userInfo = userInfoService.selectUserInfoByUsername(friendname);
		
		modelAndView.addObject("friendInfo", userInfo);
		
		modelAndView.setViewName("friendInfo");
		
		return modelAndView;
	}
	
	/**
	 * 更改用户信息的查找
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryUserInfoChange.action")
	public ModelAndView queryUserInfoChange(HttpSession session) throws Exception {
		
		ModelAndView modelAndView = new ModelAndView();
		
		UserInfo userInfo = new UserInfo();
		
		String username = (String) session.getAttribute("username");
		
		userInfo = userInfoService.selectUserInfoByUsername(username);
		
		modelAndView.addObject("userInfo", userInfo);
		
		modelAndView.setViewName("changeUserInfo");
		
		return modelAndView;
	}
	
	
	/**
	 * 更改用户信息
	 * @param request
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/changeUserInfo.action")
	public ModelAndView updateUserInfoByChange(HttpServletRequest request, HttpSession session) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		
		UserInfo userInfo = new UserInfo();
		
		userInfo.setUsername((String) session.getAttribute("username"));
		userInfo.setAlbumNumber(Integer.parseInt(request.getParameter("albumNumber")));
		
		userInfo.setRealName(request.getParameter("realname"));
		String age = request.getParameter("selectedage");
		if(!age.equals("请选择")) {
			userInfo.setAge(age);
		}
		String sex = request.getParameter("selectedsex");
		if(!sex.equals("请选择")) {
			userInfo.setSex(sex);
		}
		
		userInfo.setAddress(request.getParameter("address"));
		userInfo.setTel(request.getParameter("tel"));
		userInfo.setEmail(request.getParameter("email"));
		userInfo.setQq(request.getParameter("qq"));
		userInfo.setPersonalsay(request.getParameter("personalsay"));
		
		userInfoService.updateUserInfoByChange(userInfo);
		
		modelAndView.setViewName("personal");
		
		
		
		return modelAndView;
	}
	
	
	/**
	 * 更改密码
	 * @param request
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/changePassword.action")
	public ModelAndView changePassword(HttpServletRequest request, HttpSession session) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		
		String username =  (String) session.getAttribute("username");
		
		String oldPassword = userService.selectPassword(username);
		
		String newPassword = request.getParameter("newpassword");
		String paraOldPassword = request.getParameter("oldpassword");
		
		if(!oldPassword.equals(paraOldPassword)) {
			modelAndView.addObject("olderror", "输入的原密码有误，请重试或联系管理员。");
			modelAndView.setViewName("changePassword");
		}
		
		else {
			User user = new User();
			user.setUsername(username);
			user.setPassword(newPassword);
			userService.changePassword(user);
			
			modelAndView.setViewName("personal");
		}
		
		
		return modelAndView;
	}

}
